<?php namespace Siyuanliu\Filmcms\Controllers;

use BackendMenu;
use Backend\Classes\Controller;
use Input;
use Snoopy\Snoopy;
use QL\QueryList;
/**
 * Films Back-end Controller
 */
class Films extends Controller
{
  // public $layout = 'default';

    public $implement = [
        'Backend.Behaviors.FormController',
        'Backend.Behaviors.ListController'
    ];

    public $formConfig = 'config_form.yaml';
    public $listConfig = 'config_list.yaml';

    public function __construct()
    {
        parent::__construct();

        BackendMenu::setContext('Siyuanliu.Filmcms', 'filmcms', 'films');
    }

    public function create()
    {
        BackendMenu::setContextSideMenu('new_post');

        // $this->bodyClass = 'compact-container';
        // $this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-preview.css');
        // $this->addJs('/plugins/rainlab/blog/assets/js/post-form.js');
        $this->vars['title'] = 123124;
        return $this->asExtension('FormController')->create();
    }
    public function gather()
    {
      $this->vars['result'] = '';
      $this->pageTitle = 'gather';
      return  $this->makePartial('gather');

        // BackendMenu::setContextSideMenu('gather');
        // echo 'gather';die;
        // $this->bodyClass = 'compact-container';
        // $this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-preview.css');
        // $this->addJs('/plugins/rainlab/blog/assets/js/post-form.js');

        // return $this->asExtension('FormController')->create();
    }

    public function gatherFilm($title)
    {
      $snoopy = new Snoopy();
      $formvars['wd']= $title;
      $formvars['sumbit']='search';
      $baseUrl = 'http://c.filmyun.cn/';
      $action = "http://c.filmyun.cn/index.php?m=vod-search";//表单提交地址
      $query = $snoopy->submit($action,$formvars);//$formvars为提交的数组
      $rules = array(
          //采集id为one这个元素里面的纯文本内容
          'title' => array('.xing_vb4>a','text'),
          //采集class为two下面的超链接的链接
          'href' => array('.xing_vb4>a','href'),
          // 'link' => array('.two>a','href'),
          // //采集class为two下面的第二张图片的链接
          // 'img' => array('.two>img:eq(1)','src'),
          // //采集span标签中的HTML内容
          // 'other' => array('span','html')
      );
      $detailsRules = array(
        //标题
        'title' => array('.vodh>h2','text'),
        //头像图片
        'avatar' => array('.lazy','src'),
        // 导演
        'director'=> array('.vodinfobox>ul>li:eq(0)>span','text'),
        // 演员
        'actor'=> array('.vodinfobox>ul>li:eq(1)>span','text'),
        // 分类
        'category'=> array('.vodinfobox>ul>li:eq(2)>span','text'),
        // 语言
        'language'=> array('.vodinfobox>ul>li:eq(3)>span','text'),
        // 地区
        'location'=> array('.vodinfobox>ul>li:eq(4)>span','text'),
        // 发行时间
        'publishDate'=> array('.vodinfobox>ul>li:eq(5)>span','text'),
        // rename
        'rename'=> array('.vodinfobox>ul>li:eq(6)>span','text'),
        //更新时间
        'updateDate'=> array('.vodinfobox>ul>li:eq(7)>span','text'),
        //描述
        'description'=>array('.vodplayinfo:first','text'),
      );
      $resourceRules = array(
        //播放源类型
        'resourcesType'=>array('.vodplayinfo>div>h3','text'),
        //播放源值
        'resources'=>array('.vodplayinfo>div>ul>li>input','value'),
      );
      $data = QueryList::Query($query->results,$rules)->data;
      $result = Array();
      $i = 0;
      foreach ($data as $list) {
        $filmlist =  $snoopy->fetch($baseUrl . $list['href']);
        $result[$i] = QueryList::Query($query->results,$detailsRules)->data[0];
        $resources = QueryList::Query($query->results,$resourceRules)->data;
        $result[$i]['resources'] = $resources;
        $i++;
      }
      return $result;
    }

    public function onGather()
    {
      if (!Input::has('name')) {
          throw new \ValidationException(['name' => 'Sorry that username is already taken!']);
      }
      $result = $this->gatherFilm(Input::get('name'));
      //  print_r($result);die;
       return $this->vars['result'] = $result;
    }

    public function onGetData()
    {
      $result = $this->gatherFilm(Input::get('name'));
      $filmDetails = $result[Input::get('select')];
      return $this->vars['result'] = $filmDetails;
    }
}
